package com.jnxx.task.WxUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;



@Component
public class PushUtils {

@Autowired
    private RestTemplate restTemplate = new RestTemplate();
    @Autowired
    private TokenUtils tokenUtils;

    public ResponseEntity<Map> okrPush(String touser,String title,String content,String OKR){
        String url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+tokenUtils.getAccessToken();
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("touser", touser);        //"UserID1|UserID2|UserID3" is ok
        requestBody.put("msgtype", "textcard");
        requestBody.put("agentid", 1000033);
        requestBody.put("enable_duplicate_check", 1);
        Map<String, Object> textcard = new HashMap<>();
        textcard.put("title", title);

        LocalDate today = LocalDate.now();
        String formattedDate = today.format(DateTimeFormatter.ofPattern("yyyy年M月d日"));

        textcard.put("description", "<div class=\"gray\">"+formattedDate+"</div> <div class=\"normal\">"+content+"</div><div class=\"highlight\">"+OKR+"</div>");
        textcard.put("url", "http://qy.llczy.com/api/home");
        textcard.put("btntxt", "更多");

        requestBody.put("textcard", textcard);
        return restTemplate.postForEntity(url,requestBody,Map.class);
    }

    @Scheduled(cron = "0 32 13 * * *")
    public void yourTask() {

    }
}
